<?php
include_once 'meekrodb.class.php';
DB::$host = 'localhost'; //defaults to localhost if omitted
DB::$port = '3306'; // defaults to 3306 if omitted
DB::$encoding = 'utf8'; // defaults to latin1 if omitted
DB::$dbName = 'ltrvani';
DB::$user = 'root';
DB::$password = '';

function getLotteryByDay($day, $type){

	$sql = "SELECT l.`id`, l.`name`, l.`type`, l.`url`, l.`url1`, l.`url2`"
			." \nFROM `l_channel` l INNER JOIN `l_channel_schedule` s ON l.id = s.channel_id"
			." \nWHERE s.day = %d ";
	if($type){
		$sql .= ' AND l.`type` = %s';
	}

	return DB::query($sql, $day, $type);
}

function insertLotteryResult($channelId, $result){

	// Delete old result
	$sqlDelete = "DELETE FROM `l_channel_result`"
			." \nWHERE `channel_id` = %d"
			." \n AND `open_date` = %s";

	DB::query($sqlDelete, $channelId, $result['open_date']);

	// Insert
	$detail = $result['rawdetail'];
	for($i=0, $count = sizeof($detail);$i< $count ; $i++)	{

		$d=$detail[$i];
		$prizes=explode('-', $d);
		$prizes=array_map('trim', $prizes);
		foreach($prizes as $p)		{			
			$dataInserting=array(
					'channel_id'=>$channelId,
					'open_date' =>$result['open_date'],
					'prize'=>$i,
					'result'=> $p
			);
			DB::insert('l_channel_result',$dataInserting);			
		}
	}
}

function hasLotteryResult($channelId,$open_date){
	$sql = 'SELECT 1 FROM `l_channel_result` WHERE channel_id= %d and `open_date` = %s LIMIT 0, 1';
	$result =  DB::query($sql, $channelId, $open_date);
	return sizeof($result)>0;
}

?>